home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_077 / samples / crypt.d < prev    next >
Text File  |  1992-05-06  |  3KB  |  148 lines

  1. #include:util.g
  2.  
  3. channel input binary ChIn;
  4. channel output binary ChOut;
  5.  
  6. file() FIn, FOut;
  7.  
  8. [256] byte Key;
  9. [256] char CharKey @ Key;
  10. [256] byte StartKey;
  11.  
  12. [256] ushort Table1, Table2;
  13.  
  14. bool EnCrypt;
  15.  
  16. proc process()void:
  17.     [256] byte buffer1, buffer2;
  18.     [128] byte shortBuff;
  19.     ushort i;
  20.     bool done;
  21.  
  22.     done := false;
  23.     while not done do
  24.     if read(ChIn; shortBuff) then
  25.         for i from 0 upto 128 - 1 do
  26.         buffer1[i] := shortBuff[i];
  27.         od;
  28.         if read(ChIn; shortBuff) then
  29.         for i from 0 upto 128 - 1 do
  30.             buffer1[i + 128] := shortBuff[i];
  31.         od;
  32.         else
  33.         done := true;
  34.         fi;
  35.         for i from 0 upto 256 - 1 do
  36.         if EnCrypt then
  37.             buffer2[i] := buffer1[Table1[i]] >< Key[i];
  38.         else
  39.             buffer2[i] := buffer1[Table2[i]] >< Key[Table2[i]];
  40.         fi;
  41.         od;
  42.         if not write(ChOut; buffer2) then
  43.         write(" bad write to output file.");
  44.         exit(1);
  45.         fi;
  46.         for i from 0 upto 256 - 1 do
  47.         Key[i] := Key[i] * 19 + 37;
  48.         od;
  49.     else
  50.         done := true;
  51.     fi;
  52.     od;
  53. corp;
  54.  
  55. proc fixKey()void:
  56.     ushort i, j, s;
  57.     byte b;
  58.  
  59.     s := 256 - 1;
  60.     while s ~= 0 and CharKey[s] = ' ' do
  61.     s := s - 1;
  62.     od;
  63.     i := s + 1;
  64.     j := 0;
  65.     while
  66.     CharKey[i] := CharKey[j];
  67.     if j = s then
  68.         j := 0;
  69.     else
  70.         j := j + 1;
  71.     fi;
  72.     i ~= 256 - 1
  73.     do
  74.     i := i + 1;
  75.     od;
  76.     b := 0;
  77.     for i from 1 upto s do
  78.     b := b + Key[i];
  79.     od;
  80.     for i from 0 upto 256 - 1 do
  81.     Key[i] := Key[i] >< b;
  82.     b := b * 13 + 59;
  83.     od;
  84.     StartKey := Key;
  85.     for i from 0 upto 256 - 1 do
  86.     Table1[i] := b;
  87.     Table2[b] := i;
  88.     b := b * 17 + 43;
  89.     od;
  90. corp;
  91.  
  92. proc main()void:
  93.     *char par, p, q;
  94.     [100] char nameIn, nameOut;
  95.  
  96.     par := GetPar();
  97.     if par ~= nil and par* = '-' then
  98.     par := par + 1;
  99.     fi;
  100.     if par = nil or par* ~= 'd' and par* ~= 'e' or (par + 1)* ~= '\e' then
  101.     writeln("Use is: crypt -{d|e} file1 ... fileN");
  102.     else
  103.     EnCrypt := par* = 'e';
  104.     par := GetPar();
  105.     if par = nil then
  106.         writeln("Use is: crypt -{d|e} file1 ... fileN");
  107.     else
  108.         write("Key> ");
  109.         if readln(CharKey) then
  110.         fixKey();
  111.         while
  112.             write(par, ':');
  113.             CharsCopy(&nameIn[0], par);
  114.             CharsCopy(&nameOut[0], par);
  115.             if EnCrypt then
  116.             CharsConcat(&nameOut[0], ".CRP");
  117.             else
  118.             CharsConcat(&nameIn[0], ".CRP");
  119.             fi;
  120.             if open(ChIn, FIn, &nameIn[0]) then
  121.             if FileDestroy(&nameOut[0]) then fi;
  122.             if FileCreate(&nameOut[0]) then
  123.                 if open(ChOut, FOut, &nameOut[0]) then
  124.                 process();
  125.                 Key := StartKey;
  126.                 if not close(ChOut) then
  127.                     write(" error closing output file");
  128.                 fi;
  129.                 else
  130.                 write(" can't open output file");
  131.                 fi;
  132.             else
  133.                 write(" can't create output file");
  134.             fi;
  135.             close(ChIn);
  136.             else
  137.             write(" can't open input file");
  138.             fi;
  139.             writeln();
  140.             par := GetPar();
  141.             par ~= nil
  142.         do
  143.         od;
  144.         fi;
  145.     fi;
  146.     fi;
  147. corp;
  148.